<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>K3Cloud | 批量开单</title>
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../../static/build/css/app.min.css" th:href="${#fn.getStaticFile('/build/css/app.min.css')}">
    <link rel="stylesheet" href="../../static/lib/plugins/handsontable/handsontable.full.css" th:href="${#fn.getStaticFile('/lib/plugins/handsontable/handsontable.full.css')}"/>
    <link rel="stylesheet" href="../../static/dist/css/common.css" th:href="${#fn.getStaticFile('/dist/css/common.css')}">
    <link rel="stylesheet" href="../../static/lib/plugins/bootstrap-daterangepicker/daterangepicker.css"  th:href="@{/lib/plugins/bootstrap-daterangepicker/daterangepicker.css}" type="text/css" media="all"/>
    <script src="../../static/build/js/app.min.js" th:src="${#fn.getStaticFile('/build/js/app.min.js')}"></script>
    <script src="../../static/lib/plugins/jquery-validation/1.11.1/jquery.validate.min.js" th:src="${#fn.getStaticFile('/lib/plugins/jquery-validation/1.11.1/jquery.validate.min.js')}"></script>
    <script src="../../static/lib/plugins/handsontable/handsontable.full.js" th:src="${#fn.getStaticFile('/lib/plugins/handsontable/handsontable.full.js')}"></script>
    <script src="../../static/lib/plugins/jquery.form.js" th:src="${#fn.getStaticFile('/lib/plugins/jquery.form.js')}"></script>
    <script src="../../static/dist/js/common.js" th:src="${#fn.getStaticFile('/dist/js/common.js')}"></script>
    <script src="../../static/lib/plugins/bootstrap-daterangepicker/moment.js" th:src="${#fn.getStaticFile('/lib/plugins/bootstrap-daterangepicker/moment.js')}" type="text/javascript"></script>
    <script src="../../static/lib/plugins/bootstrap-daterangepicker/daterangepicker.js" th:src="${#fn.getStaticFile('/lib/plugins/bootstrap-daterangepicker/daterangepicker.js')}" type="text/javascript"></script>
    <script th:inline="javascript">
        $(document).ready(function () {
            $("#btnSubmit").click(function () {
                $("#btnSubmit").attr("disabled", true);
                $("#inputForm").submit();
            });
            var customerNames = [[${customerNames}]];
            var productNames = [[${productNames}]];
            var types = [[${types}]];
            $("#grid").handsontable({
                height: 650,
                rowHeaders: true,
                colHeaders: ["门店", "货品", "价格", "数量", "类型", "备注"],
                columns: [
                    {type: "autocomplete", allowEmpty: false, strict: true, source: customerNames},
                    {type: "autocomplete", allowEmpty: false, strict: true, source: productNames},
                    {type: 'numeric',allowEmpty: false,format:"0,0.00"},
                    {type: "numeric", allowEmpty: false},
                    {type: "autocomplete", allowEmpty: false, strict: true, source: types},
                    {type: "text", allowEmpty: true, strict: true }
                ],
                stretchH: 'all',
                contextMenu: ['row_above', 'row_below', 'remove_row'],
                afterChange: function (changes, source) {
                    var ht = $('#grid').handsontable('getInstance');
                    ht.validateNotEmptyRows(function () {
                        ht.render();
                    });
                }
            });
            $("#inputForm").validate({
                invalidHandler: function (event, validator) {
                    $("#btnSubmit").attr("disabled", false);
                },
                submitHandler: function (form) {
                    var invalidCount = $("#grid .htInvalid").length;
                    if(invalidCount > 0){
                        $("#btnSubmit").attr("disabled",false);
                        alert("请先处理错误信息。");
                        return
                    }else{
                        var ht = $('#grid').handsontable('getInstance');
                        var datas = JSON.stringify(ht.getNotEmptyData());
                        if(ht.getNotEmptyData()==''){
                            alert("没有填写任何数据！");
                            $("#btnSubmit").attr("disabled", false);
                        }else{
                            if (confirm("确认保存？")) {
                                $("#btnSubmit").attr("disabled", true);
                                $("#data").val(datas);
                                form.submit();
                            } else {
                                $("#btnSubmit").attr("disabled", false);
                            }
                        }

                    }
                }
            });
            $("#companyName").on("change", function () {
                window.location.href=ctx+"cloud/batchBill/form?companyName="+$("#companyName").val();
            })
        });
    </script>
</head>
<body class="hold-transition skin-blue sidebar-mini " th:classappend="${#session.getAttribute('sidebarClass')}" th:with="pageCode='batchBillForm'">
    <div class="wrapper">
    <!-- Main Header -->
    <div th:replace="fragment/layout :: header"></div>
    <!-- Left side column. contains the logo and sidebar -->
    <div th:replace="fragment/layout :: menu(activePageCode=${pageCode})"></div>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Main content -->
        <section class="content">
            <!-- Nav tabs -->
            <ul th:replace="fragment/menuTab :: menuTab(activePageCode=${pageCode})"></ul>
            <div class="panel panel-info">
                <div th:if="${message!=null}"  th:class="'alert alert-'+${message.type}" th:text="${message.code}"></div>
                <div class="panel-body">
                    <form class="form-inline pull-left" id="inputForm" role="form" th:action="@{/cloud/batchBill/save}" method="post">
                        <input type="hidden" name="token"  th:value="${token}"/>
                        <input type="hidden" id="data" name="data" value=""/>
                        <div class="form-group">
                            <label for="companyName">账套</label>
                            <select name="companyName" id="companyName" class="form-control" style="width:240px;">
                                <option th:value="null" th:text="请选择账套"></option>
                                <option th:each="item : ${futureClouds}" th:value="${item}" th:text="${item}" th:selected="${companyName==item}">cloud</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="storeCode">仓库</label>
                            <select name="storeCode" id="storeCode" class="required form-control" style="width:240px;">
                                <option th:value="null" th:text="请选择仓库"></option>
                                <option th:each="item : ${stores}" th:value="${item.fnumber}" th:text="${item.fname}" th:selected="${storeCode==item}">cloud</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="billDate">日期</label>
                            <input type="text" id="billDate" name="billDate"  class="form-control datepicker" readonly="true"/>
                        </div>
                        <button type="button" id="btnSubmit" class="btn btn-success">
                            <span class="glyphicon glyphicon-save"></span>保存
                        </button>
                    </form>
                </div>
                <div class="panel-body">
                    <div id="grid" style="margin-top:10px;"></div>
                </div>
            </div>
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
    <div th:replace="fragment/layout :: footer"></div>
</div>
</body>
</html>